package com.spdd.mt.common.util;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.codec.binary.Hex;

import java.security.MessageDigest;


public class Md5Util {

    public static String createMD5ByStr(String str) {
        return createMD5ByStr(str, "UTF-8");
    }

    public static String createMD5ByStr(String str, String charset) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            if (StrUtil.isEmpty(charset)) {
                return Hex.encodeHexString(digest.digest(str.getBytes()));
            }
            return Hex.encodeHexString(digest.digest(str.getBytes(charset)));
        } catch (Exception e) {
            return null;
        }
    }
}
